package com.my.serviceimpl;

import com.my.bean.*;
import com.my.mapper.RelationProjectMapper;
import com.my.service.MoenyDetailedService;
import com.my.service.OrderVipService;
import com.my.service.ProductionService;
import com.my.service.RelationProjectService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产康项目关联映射表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-29
 */
@Service
public class RelationProjectServiceImpl extends ServiceImpl<RelationProjectMapper, RelationProject> implements RelationProjectService {
    @Autowired
    OrderVipService orderVipService;
    @Autowired
    ProductionService productionService;

    @Autowired
    MoenyDetailedService moenyDetailedService;
    @Override
    public List<Map<String, Object>> findCount(Long areaid,boolean admin) {
        return baseMapper.findCount(areaid,admin);
    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String insertProjectRelation(RelationProject relationProject) throws SQLException {
        relationProject.setCreateTime(new Date().getTime());
        relationProject.setIsState(2);
        if(this.insert(relationProject)){
            //先根据房间ID查询出 订单ID
            Map map=new HashMap();
            map.put("home_id",relationProject.getRelationId());
            map.put("whether_home",2);
            List<OrderVip> orderVips= orderVipService.selectByMap(map);
            if (!(orderVips.size()>0)){
                throw  new SQLException();
            }
            Long orderId=orderVips.get(0).getId();
            Map map2=new HashMap();
            map2.put("id",relationProject.getProjectId());
            List<Production> productions = productionService.selectByMap(map2);
            Production production=productions.get(0);

            //再把产康名字和产康ID 价格  房间ID 订单ID 创建时间 保存在交易明细表中
            MoenyDetailed moenyDetailed=new MoenyDetailed();
            moenyDetailed.setOrderId(orderId);
            moenyDetailed.setMoney(Integer.parseInt(production.getPice()+""));
            moenyDetailed.setCreateTime(new Date().getTime());
            moenyDetailed.setProjectType(production.getProject());
            moenyDetailed.setProjectTypeId(production.getId());
            if(moenyDetailedService.insert(moenyDetailed)) {
                return  "预约成功";
            }
        }
        throw  new SQLException();
    }

    @Override
    public String insertRelationProject2(Long projectId)  {
        Map map2=new HashMap();
        map2.put("id",projectId);
        List<Production> productions = productionService.selectByMap(map2);

        MoenyDetailed moenyDetailed=new MoenyDetailed();
        moenyDetailed.setMoney(Integer.parseInt(productions.get(0).getPice()+""));
        moenyDetailed.setCreateTime(new Date().getTime());
        moenyDetailed.setProjectType(productions.get(0).getProject());
        if(moenyDetailedService.insert(moenyDetailed)){
            return "预约成功";
        }
        return  "预约失败";
    }

    @Override
    public ResultPage<RelationProject> findProjectByUserName(String userName, VoPage voPage,Long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userName", userName);
        List<RelationProject> list = baseMapper.findProjectByUserName(paramMap,areaid,admin);
        Integer count = baseMapper.findProjectByUserNameCount(paramMap,areaid,admin);
        ResultPage<RelationProject> resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


}
